#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
pthread_t ntid1, ntid2, ntid3;
int c = 0;
int temp = 0;

void *reader1(void *para)
{
    while (1)
    {
        if (temp != c)
        {
            temp = c;
            printf("reader: value of c = %d\n", c);
            fflush(stdout);
        }
    }
    return ((void *)0);
}

void *reader2(void *para)
{
    while (1)
    {
        printf("watch2 c = %d, temp = %d\n", c, temp);
        fflush(stdout);
        usleep(100000);
    }
    return ((void *)0);
}

void *writer1(void *para)
{
    for (int i = 0; i < 5; i++)
    {
        c++;
        printf("writer: changed value to = %d\n", c);
        fflush(stdout);
        sleep(1);
    }
    return ((void *)0);
}

int main()
{
    int err;
    err = pthread_create(&ntid1, NULL, reader1, NULL);
    if (err != 0)
    {
        printf("can’t create thread: %s\n", strerror(err));
        return 1;
    }
    err = pthread_create(&ntid2, NULL, reader2, NULL);
    if (err != 0)
    {
        printf("can’t create thread: %s\n", strerror(err));
        return 1;
    }
    err = pthread_create(&ntid3, NULL, writer1, NULL);
    if (err != 0)
    {
        printf("can’t create thread: %s\n", strerror(err));
        return 1;
    }
    sleep(6);
    return 0;
}
